//
//  HLSingleton.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/26.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLSingleton.h"

static HLSingleton * hlSingleton;

@interface HLSingleton()

@property(nonatomic,copy)NSString* plistPath;

@end

@implementation HLSingleton

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    // 一次函数
    dispatch_once(&onceToken, ^{
        if (hlSingleton == nil) {
            hlSingleton = [super allocWithZone:zone];
        }
    });
    return hlSingleton;
}

+ (instancetype)share {
    return [[self alloc] init];
}

- (instancetype)init {
    self = [super init];
    if (self) {

    }
    return self;
}

- (void)savePackingWayPlist:(NSArray*)array {
    NSString* path = [self.plistPath stringByAppendingPathComponent:@"packingway.plist"];
    [NSKeyedArchiver archiveRootObject:array toFile:path];
    self.packingWays = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

- (void)savePaymentWayPlist:(NSArray*)array {
    NSString* path = [self.plistPath stringByAppendingPathComponent:@"paymentway.plist"];
    [NSKeyedArchiver archiveRootObject:array toFile:path];
    self.paymentWays = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

- (void)saveServicePlist:(NSArray*)array {
    NSString* path = [self.plistPath stringByAppendingPathComponent:@"service.plist"];
    [NSKeyedArchiver archiveRootObject:array toFile:path];
    self.serviceTypes = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

- (void)saveTransportTypePlist:(NSArray*)array {
    NSString* path = [self.plistPath stringByAppendingPathComponent:@"transport.plist"];
    [NSKeyedArchiver archiveRootObject:array toFile:path];
    self.transportTypes = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

- (void)saveAdditionalPlist:(NSArray*)array {
    NSString* path = [self.plistPath stringByAppendingPathComponent:@"additional.plist"];
    [NSKeyedArchiver archiveRootObject:array toFile:path];
    self.additionals = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

- (NSString*)plistPath {
    if (!_plistPath) {
        _plistPath = [docSandBoxPath stringByAppendingPathComponent:@"plist"];
        if (![[NSFileManager defaultManager]fileExistsAtPath:_plistPath]) {
            [[NSFileManager defaultManager]createDirectoryAtPath:_plistPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    return _plistPath;
}

@end
